package com.skeleton.show.control;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skeleton.db.dto.ShowinfoDto;
import com.skeleton.show.service.ShowService;

/**
 * AdminControl.java
 * 
 * @author Wangyu
 */
@Controller
@RequestMapping("admin/")
public class AdminControl {

	protected Logger logger = LogManager.getLogger(this.getClass());

	@Resource
	private ShowService showService;

	@RequestMapping("admin")
	public String admin(HttpServletRequest request) {
		logger.info("-----------admin---------admin--------------------");
		List<ShowinfoDto> list = showService.getAllShow();
		request.setAttribute("list", list);
		return "admin";
	}

	@RequestMapping("addmain")
	public String addmain(HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("-----------admin---------addmain--------------------");
		request.setCharacterEncoding("utf-8");
		String mainName = request.getParameter("mainName");

		ShowinfoDto sDto = new ShowinfoDto();
		sDto.setShowTitle(mainName);
		sDto.setShowStatus('0');

		showService.doAddMain(sDto);
		response.sendRedirect("admin");
		return null;
	}

	@RequestMapping("addsub")
	public String addsub(HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("-----------admin---------addsub--------------------");
		request.setCharacterEncoding("utf-8");
		String mainId = request.getParameter("mainId");
		String subName = request.getParameter("subName");

		ShowinfoDto sDto = new ShowinfoDto();
		sDto.setShowTitle(subName);
		sDto.setShowStatus('0');

		showService.doAddSub(mainId, sDto);
		response.sendRedirect("admin");
		return null;
	}

	@RequestMapping("additem")
	public String additem(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
		logger.info("-----------admin---------additem--------------------");
		request.setCharacterEncoding("utf-8");
		String subId = request.getParameter("subId");
		String itemName = request.getParameter("itemName");
		itemName = itemName.replaceAll("\n", "<br />");
		String desc = request.getParameter("desc");
		desc = desc.replaceAll("\n", "<br />");
		String people = request.getParameter("people");
		String start = request.getParameter("start");
		String end = request.getParameter("end");

		ShowinfoDto sDto = new ShowinfoDto();
		sDto.setShowTitle(itemName);
		sDto.setShowCommnet(desc);
		sDto.setShowStatus('0');
		sDto.setShowPeople(people);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		if (start != null && !start.equals("")) {
			sDto.setStartDttm(sdf.parse(start));
		}
		if (end != null && !end.equals("")) {
			sDto.setEndDttm(sdf.parse(end));
		}

		showService.doAddItem(subId, sDto);
		response.sendRedirect("admin");
		return null;
	}

	@RequestMapping("modifyItemInfo")
	@ResponseBody
	public Map<String, Object> modifyItemInfo(HttpServletRequest request) {
		logger.info("-----------admin---------modifyItemInfo--------------------");
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		ShowinfoDto sDto = new ShowinfoDto();
		sDto.setShowId(id);

		ShowinfoDto dto = showService.getItemById(sDto);
		map.put("result", "success");
		map.put("dto", dto);
		return map;
	}

	@RequestMapping("modifyitem")
	public String modifyitem(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
		logger.info("-----------admin---------modifyitem--------------------");
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");
		String itemName = request.getParameter("itemName");
		itemName = itemName.replaceAll("\n", "<br />");
		String itemDesc = request.getParameter("itemDesc");
		itemDesc = itemDesc.replaceAll("\n", "<br />");
		String itemStatus = request.getParameter("itemStatus");
		String isUpdate = request.getParameter("isUpdate");
		String people = request.getParameter("people");
		String start = request.getParameter("start");
		String end = request.getParameter("end");

		ShowinfoDto sDto = new ShowinfoDto();
		sDto.setShowId(id);
		sDto.setShowTitle(itemName);
		sDto.setShowCommnet(itemDesc);
		sDto.setShowStatus(itemStatus.charAt(0));
		sDto.setShowPeople(people);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		if (start != null && !start.equals("")) {
			sDto.setStartDttm(sdf.parse(start));
		}
		if (end != null && !end.equals("")) {
			sDto.setEndDttm(sdf.parse(end));
		}

		showService.doModifyItem(sDto, isUpdate);
		response.sendRedirect("admin");
		return null;
	}

	@RequestMapping("deleteItem")
	public String deleteItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("-----------admin---------deleteItem--------------------");
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");

		ShowinfoDto sDto = new ShowinfoDto();
		sDto.setShowId(id);

		showService.doDeleteItem(sDto);
		response.sendRedirect("admin");
		return null;
	}

	@RequestMapping("deleteSub")
	public String deleteSub(HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("-----------admin---------deleteSub--------------------");
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");

		ShowinfoDto sDto = new ShowinfoDto();
		sDto.setShowId(id);

		showService.doDeleteSub(sDto);
		response.sendRedirect("admin");
		return null;
	}

	@RequestMapping("deleteMain")
	public String deleteMain(HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("-----------admin---------deleteMain--------------------");
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");

		ShowinfoDto sDto = new ShowinfoDto();
		sDto.setShowId(id);

		showService.doDeleteMain(sDto);
		response.sendRedirect("admin");
		return null;
	}

	@RequestMapping("deleteAllDate")
	public String deleteAllDate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("-----------admin---------deleteAllDate--------------------");
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");

		ShowinfoDto sDto = new ShowinfoDto();
		sDto.setShowId(id);

		showService.doDeleteAllDate(sDto);
		response.sendRedirect("admin");
		return null;
	}

}
